package org.example.lc;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

class p283 {
    /**
     * max len
     * sub : [0] = 0
     * [1] = 1
     */
    class Solution {

        /**
         * nextzero不断向前
         * @param nums
         */
        public void swap(int[] nums,int i,int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
        public void moveZeroes(int[] nums) {
            int nextZero = -1;
            int nextNoZero = -1;
            for(int i=0;i<nums.length;i+=1) {
                if(nums[i]==0) {
                    nextZero = i;
                    break;
                }
            }
            if(nextZero==-1) {
                return;
            }
            for(int i=nextZero;i<nums.length;i+=1) {
                if(nums[i]!=0) {
                    nextNoZero = i;
                    break;
                }
            }
//            for(int i=0;i<nums.length;i+=1) {
//                if(nextZero!=-1&&nextNoZero!=-1) {
//                    break;
//                }
//                if(nums[i]==0&&nextZero==-1) {
//                    nextZero=i;
//                }
//                if(nums[i]!=0&&nextNoZero==-1) {
//                    nextNoZero=i;
//                }
//
//            }

            while(nextZero<nextNoZero&&nextNoZero>0) {
                swap(nums,nextZero,nextNoZero);
                for(int i=nextZero;i<nums.length;i+=1) {
                    if(nums[i]==0) {
                        nextZero = i;
                        break;
                    }
                }
                nextNoZero=-1;
                // next no zero
                for(int i=nextZero;i<nums.length;i+=1) {
                    if(nums[i]!=0) {
                        nextNoZero = i;
                        break;
                    }
                }

            }

        }
    }
    @Test
    public void d1() {
//        assertEquals(4, new Solution().longestConsecutive(new int[]{100,4,200,1,3,2}));
//        assertEquals(9, new Solution().longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));
        int[] raw = new int[]{0,1,0,3,12};
        //1,0,0,3,12
        new Solution().moveZeroes(raw);
        assertArrayEquals(new int[]{1,3,12,0,0}, raw);

    }
    @Test
    public void d3() {
//        assertEquals(4, new Solution().longestConsecutive(new int[]{100,4,200,1,3,2}));
//        assertEquals(9, new Solution().longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));
        int[] raw = new int[]{1,0};
        new Solution().moveZeroes(raw);
        assertArrayEquals(new int[]{1,0}, raw);

    }
    @Test
    public void d2() {
//        assertEquals(4, new Solution().longestConsecutive(new int[]{100,4,200,1,3,2}));
//        assertEquals(9, new Solution().longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}));
        int[] raw = new int[]{1, 0, 0};
        new Solution().moveZeroes(raw);
        assertArrayEquals(new int[]{1, 0, 0}, raw);
    }
    @Test
    public void d4() {
        int[] raw = new int[]{0};
        new Solution().moveZeroes(raw);
        assertArrayEquals(new int[]{ 0}, raw);
    }
    @Test
    public void d5() {
        int[] raw = new int[]{1,0,1};
        new Solution().moveZeroes(raw);
        assertArrayEquals(new int[]{ 1,1,0}, raw);
    }

    @Test
    public void d7() {
        int[] raw = new int[]{1};
        new Solution().moveZeroes(raw);
        assertArrayEquals(new int[]{ 1}, raw);
    }
}
